/*本模块功能:图标管理
        编写者:杨锡君
        编写时间:20180717*/
$(function() {
    var Alert1 = new Alert();
    var loading1 = new Loading();//监控全局ajax添加loading
    loading1.init();
    var confirm1 = new Confirm();

    var index = {
        schIconId:0,
        init: function() {
            this.bind();
        },
        getData:function () {
            var self = this;
            loading1.show();
            $.ajax({
                type:'post',
                url:'getSchIconInfo',
                async: false,
                success:function (msg) {
                    loading1.hide();
                    msg = JSON.parse(msg);
                    if(msg.result == "success") {
                        var schIconId = 0;
                        if(msg.schIconDetail != null){
                            $(".someBox").show();
                            $(".fileBox").css("background","none");
                            schIconId =  msg.schIconDetail.schIconId;
                            $(".name").html(msg.schIconDetail.schoolIcon);
                            $(".name").attr("title",msg.schIconDetail.schoolIcon);
                            $(".time").html(timeObject.changedateYear(msg.schIconDetail.updateTime));
                            $(".icoImg").html("<img  src='"+ msg.schoolIconUrl + "/" + msg.schIconDetail.schoolIcon +"' >");
                            if(msg.schIconDetail.mark == 1){
                                $(".make").next("label").addClass("checked").siblings("label").removeClass("checked");
                                $(".rightBox").html("<img  src='"+msg.schoolIconUrl + "/" + msg.schIconDetail.schoolIcon +"' >");
                            }else{
                                $(".stop").next("label").addClass("checked").siblings("label").removeClass("checked");
                                $(".rightBox").html("");
                            }
                        }else{
                            $(".stop").next("label").addClass("checked").siblings("label").removeClass("checked");
                        }
                        $(".make").off("click").click(function () {
                            if($(".name").html() == ""){
                                Alert1.content = "请先上传图标";
                                Alert1.init();
                                Alert1.show();
                            }else {
                                loading1.show();
                                var json = JSON.stringify({"mark":1,"schIconId":schIconId});
                                $.ajax({
                                    type: 'post',
                                    url: 'saveSchMark',
                                    async: false,
                                    data: {
                                        "jsonStr": json
                                    },
                                    success: function (msg) {
                                        loading1.hide();
                                        msg = JSON.parse(msg);
                                        if (msg.result == "success") {
                                            self.getData();
                                        } else {
                                            Alert1.content = msg.failMsg;
                                            Alert1.init();
                                            Alert1.show();
                                        }
                                    }
                                });
                            }
                        });
                        $(".stop").off("click").click(function () {
                            if($(".name").html() == ""){
                                Alert1.content = "请先上传图标";
                                Alert1.init();
                                Alert1.show();
                            }else {
                                var json = JSON.stringify({"mark":0,"schIconId":schIconId});
                                loading1.show();
                                $.ajax({
                                    type: 'post',
                                    url: 'saveSchMark',
                                    async: false,
                                    data: {
                                        "jsonStr": json
                                    },
                                    success: function (msg) {
                                        loading1.hide();
                                        msg = JSON.parse(msg);
                                        if (msg.result == "success") {
                                            self.getData();
                                        } else {
                                            Alert1.content = msg.failMsg;
                                            Alert1.init();
                                            Alert1.show();
                                        }
                                    }
                                });
                            }
                        });
                        self.fileEven(schIconId);
                    }else{
                        Alert1.content = msg.failMsg;
                        Alert1.init();
                        Alert1.show();
                    }
                }
            });
        },
        fileEven:function (schIconId) {
            var self = this;
            $("#file").off("change").change(function() {
                var formData = new FormData($("form")[0]);

                formData.append("schIconId", schIconId);
                if($(this).val() != ""){
                    var type = $(this).val().substring($(this).val().lastIndexOf(".")).toLowerCase(); // 取文件格式后缀名
                    var flag = true;
                    var that = $(this);
                    if(flag){
                        loading1.show();

                        $.ajax({
                            url: "addSchoolIcon",
                            type: 'post',
                            data: formData,
                            async: false,
                            cache: false,
                            contentType: false,
                            processData: false,
                            success: function(msg) {
                                loading1.hide();
                                msg = JSON.parse(msg);
                                if(msg.result == "success") {
                                    self.getData();
                                } else {
                                    Alert1.content = msg.failMsg;
                                    that.val("");
                                    Alert1.init();
                                    Alert1.show();
                                }
                            }
                        });
                    }
                }


            });
        },
        bind: function() {
            var self = this;
            self.getData();
        }
    };
    index.init();

});